<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="This is a collection of PyTorch implementations/tutorials of transformers and related techniques."/>

    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:image:src" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta name="twitter:title" content="Transformers"/>
    <meta name="twitter:description" content="This is a collection of PyTorch implementations/tutorials of transformers and related techniques."/>
    <meta name="twitter:site" content="@labmlai"/>
    <meta name="twitter:creator" content="@labmlai"/>

    <meta property="og:url" content="https://nn.labml.ai/transformers/index.html"/>
    <meta property="og:title" content="Transformers"/>
    <meta property="og:image" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta property="og:site_name" content="Transformers"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="Transformers"/>
    <meta property="og:description" content="This is a collection of PyTorch implementations/tutorials of transformers and related techniques."/>

    <title>Transformers</title>
    <link rel="shortcut icon" href="/icon.png"/>
    <link rel="stylesheet" href="../pylit.css?v=1">
    <link rel="canonical" href="https://nn.labml.ai/transformers/index.html"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.13.18/dist/katex.min.css" integrity="sha384-zTROYFVGOfTw7JV7KUu8udsvW2fx4lWOsCEDqhBreBwlHI4ioVRtmIvEThzJHGET" crossorigin="anonymous">

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-4V3HC8HBLH"></script>
    <script>
        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }

        gtag('js', new Date());

        gtag('config', 'G-4V3HC8HBLH');
    </script>
</head>
<body>
<div id='container'>
    <div id="background"></div>
    <div class='section'>
        <div class='docs'>
            <p>
                <a class="parent" href="/">home</a>
                <a class="parent" href="index.html">transformers</a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations" target="_blank">
                    <img alt="Github"
                         src="https://img.shields.io/github/stars/labmlai/annotated_deep_learning_paper_implementations?style=social"
                         style="max-width:100%;"/></a>
                <a href="https://twitter.com/labmlai" rel="nofollow" target="_blank">
                    <img alt="Twitter"
                         src="https://img.shields.io/twitter/follow/labmlai?style=social"
                         style="max-width:100%;"/></a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations/tree/master/labml_nn/transformers/__init__.py" target="_blank">
                    View code on Github</a>
            </p>
        </div>
    </div>
    <div class='section' id='section-0'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-0'>#</a>
            </div>
            <h1>Transformers</h1>
<p>This module contains <a href="https://pytorch.org/">PyTorch</a> implementations and explanations of original transformer from paper <a href="https://arxiv.org/abs/1706.03762">Attention Is All You Need</a>, and derivatives and enhancements of it.</p>
<ul><li><a href="mha.html">Multi-head attention</a> </li>
<li><a href="models.html">Transformer Encoder and Decoder Models</a> </li>
<li><a href="feed_forward.html">Position-wise Feed Forward Network (FFN)</a> </li>
<li><a href="positional_encoding.html">Fixed positional encoding</a></li></ul>
<h2><a href="xl/index.html">Transformer XL</a></h2>
<p>This implements Transformer XL model using <a href="xl/relative_mha.html">relative multi-head attention</a></p>
<h2><a href="rope/index.html">Rotary Positional Embeddings</a></h2>
<p>This implements Rotary Positional Embeddings (RoPE)</p>
<h2><a href="alibi/index.html">Attention with Linear Biases</a></h2>
<p>This implements Attention with Linear Biases (ALiBi).</p>
<h2><a href="retro/index.html">RETRO</a></h2>
<p>This implements the Retrieval-Enhanced Transformer (RETRO).</p>
<h2><a href="compressive/index.html">Compressive Transformer</a></h2>
<p>This is an implementation of compressive transformer that extends upon <a href="xl/index.html">Transformer XL</a> by compressing the oldest memories to give a longer attention span.</p>
<h2><a href="gpt/index.html">GPT Architecture</a></h2>
<p>This is an implementation of GPT-2 architecture.</p>
<h2><a href="glu_variants/simple.html">GLU Variants</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/2002.05202">GLU Variants Improve Transformer</a>.</p>
<h2><a href="knn/index.html">kNN-LM</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/1911.00172">Generalization through Memorization: Nearest Neighbor Language Models</a>.</p>
<h2><a href="feedback/index.html">Feedback Transformer</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/2002.09402">Accessing Higher-level Representations in Sequential Transformers with Feedback Memory</a>.</p>
<h2><a href="switch/index.html">Switch Transformer</a></h2>
<p>This is a miniature implementation of the paper <a href="https://arxiv.org/abs/2101.03961">Switch Transformers: Scaling to Trillion Parameter Models with Simple and Efficient Sparsity</a>. Our implementation only has a few million parameters and doesn&#x27;t do model parallel distributed training. It does single GPU training but we implement the concept of switching as described in the paper.</p>
<h2><a href="fast_weights/index.html">Fast Weights Transformer</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/2102.11174">Linear Transformers Are Secretly Fast Weight Memory Systems in PyTorch</a>.</p>
<h2><a href="fnet/index.html">FNet: Mixing Tokens with Fourier Transforms</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/2105.03824">FNet: Mixing Tokens with Fourier Transforms</a>.</p>
<h2><a href="aft/index.html">Attention Free Transformer</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/2105.14103">An Attention Free Transformer</a>.</p>
<h2><a href="mlm/index.html">Masked Language Model</a></h2>
<p>This is an implementation of Masked Language Model used for pre-training in paper <a href="https://arxiv.org/abs/1810.04805">BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding</a>.</p>
<h2><a href="mlp_mixer/index.html">MLP-Mixer: An all-MLP Architecture for Vision</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/2105.01601">MLP-Mixer: An all-MLP Architecture for Vision</a>.</p>
<h2><a href="gmlp/index.html">Pay Attention to MLPs (gMLP)</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/2105.08050">Pay Attention to MLPs</a>.</p>
<h2><a href="vit/index.html">Vision Transformer (ViT)</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/2010.11929">An Image Is Worth 16x16 Words: Transformers For Image Recognition At Scale</a>.</p>
<h2><a href="primer_ez/index.html">Primer EZ</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/2109.08668">Primer: Searching for Efficient Transformers for Language Modeling</a>.</p>
<h2><a href="hour_glass/index.html">Hourglass</a></h2>
<p>This is an implementation of the paper <a href="https://arxiv.org/abs/2110.13711">Hierarchical Transformers Are More Efficient Language Models</a></p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">112</span><span></span><span class="kn">from</span> <span class="nn">.configs</span> <span class="kn">import</span> <span class="n">TransformerConfigs</span>
<span class="lineno">113</span><span class="kn">from</span> <span class="nn">.models</span> <span class="kn">import</span> <span class="n">TransformerLayer</span><span class="p">,</span> <span class="n">Encoder</span><span class="p">,</span> <span class="n">Decoder</span><span class="p">,</span> <span class="n">Generator</span><span class="p">,</span> <span class="n">EncoderDecoder</span>
<span class="lineno">114</span><span class="kn">from</span> <span class="nn">.mha</span> <span class="kn">import</span> <span class="n">MultiHeadAttention</span>
<span class="lineno">115</span><span class="kn">from</span> <span class="nn">labml_nn.transformers.xl.relative_mha</span> <span class="kn">import</span> <span class="n">RelativeMultiHeadAttention</span></pre></div>
        </div>
    </div>
    <div class='footer'>
        <a href="https://papers.labml.ai">Trending Research Papers</a>
        <a href="https://labml.ai">labml.ai</a>
    </div>
</div>
<script src=../interactive.js?v=1"></script>
<script>
    function handleImages() {
        var images = document.querySelectorAll('p>img')

        for (var i = 0; i < images.length; ++i) {
            handleImage(images[i])
        }
    }

    function handleImage(img) {
        img.parentElement.style.textAlign = 'center'

        var modal = document.createElement('div')
        modal.id = 'modal'

        var modalContent = document.createElement('div')
        modal.appendChild(modalContent)

        var modalImage = document.createElement('img')
        modalContent.appendChild(modalImage)

        var span = document.createElement('span')
        span.classList.add('close')
        span.textContent = 'x'
        modal.appendChild(span)

        img.onclick = function () {
            console.log('clicked')
            document.body.appendChild(modal)
            modalImage.src = img.src
        }

        span.onclick = function () {
            document.body.removeChild(modal)
        }
    }

    handleImages()
</script>
</body>
</html>